namespace RSSReader.DAL.Migrations
{
  using System;
  using System.Data.Entity.Migrations;
  using System.Data.Entity.Migrations.Infrastructure;

  public partial class CreateViews : DbMigration
  {
  //  public static void CreateView(this DbMigration migration,
  //string viewName, string viewqueryString)
  //  {
  //    ((IDbMigration)migration)
  //      .AddOperation(new CreateViewOperation(viewName,
  //         viewqueryString));
  //  }

    public override void Up()
    {
      this.CreateView("dbo.UserSubscriptionVW", 
          @"select  SRC.Id as SourceId,
	                  SRC.Url as Url,
	                  SUB.UserId as UserId,
	                  SUB.IsSubscribe as IsSubscribe
            from    dbo.Subscriptions SUB,
	                  dbo.Sources SRC
            where   SRC.Id = SUB.SourceId");

      this.CreateView("dbo.UserArticleVW", 
          @"select  ART.Url as Url,
            	      ART.Id as ArticleId,
                    ART.SourceId as SourceId,
            	      UART.IsFavorite as IsFavorite,
                    UART.UserID as UserId
            from    dbo.Articles ART,
            	      dbo.UserArticles UART
            where	ART.Id = UART.ArticleId");
//      ApplicationDbContext context = new ApplicationDbContext();

//      var UserSubscriptionVWScript =
//          @"CREATE VIEW dbo.UserSubcriptionVW
//            AS
//            select  SRC.Id as SourceId,
//	                  SRC.Url as Url,
//	                  SUB.UserId as UserId,
//	                  SUB.IsSubscribe as IsSubscribe
//            from    dbo.Subscriptions SUB,
//	                  dbo.Sources SRC
//            where   SRC.Id = SUB.SourceId";

//      var UserArticleVWScript =
//          @"CREATE VIEW dbo.UserArticleVW
//            AS
//            select  ART.Url as Url,
//            	      ART.Id as ArticleId,
//            	      UART.IsFavorite as IsFavorite,
//                    UART.UserID as UserId
//            from    dbo.Articles ART,
//            	      dbo.UserArticles UART
//            where	ART.Id = UART.ArticleId";

//      //Uncomment if multiple instal
//      //context.Database.ExecuteSqlCommand("DROP VIEW dbo.UserSubcriptionVW");
//      //context.Database.ExecuteSqlCommand("DROP VIEW IF EXISTS dbo.UserArticleVW");

//      context.Database.ExecuteSqlCommand(UserSubscriptionVWScript);
//      context.Database.ExecuteSqlCommand(UserArticleVWScript);
    }

    public override void Down()
    {
      ApplicationDbContext context = new ApplicationDbContext();
      context.Database.ExecuteSqlCommand("DROP VIEW dbo.UserSubscriptionVW");
      context.Database.ExecuteSqlCommand("DROP VIEW dbo.UserArticleVW");
      //this.RemoveView();
      //ApplicationDbContext context = new ApplicationDbContext();
      //context.Database.ExecuteSqlCommand("DROP VIEW dbo.UserSubcriptionVW");
      //context.Database.ExecuteSqlCommand("DROP VIEW dbo.UserArticleVW");
    }
  }
}
